import MySQLdb

# initialize DB connection
db = MySQLdb.connect(host='localhost', user='root', db='gradapp', charset='utf8', use_unicode=True)
cursor = db.cursor()

# read file and move data
file = open('../WebContent/universities.txt', 'r')
location_set = False
locatiion = ''
has_universities = False
for item in file.readlines():
	if not item.strip():
		if not location_set:
			location_set = True
		if location_set and not has_universities:
			has_universities = True
		else:
			location_set = False
			has_universities = False
	else:
		if location_set:
			location = item.strip()
		else:
			sql = """INSERT INTO Universities (name, location) VALUES (%s, %s)"""
			cursor.execute(sql, (item.strip(), location))
cursor.close()
db.commit()
